<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 汪利东,2016-04-08 15:36
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model\RelationModel;

class BookModel extends RelationModel
{
    protected $_link = [
        'book_type' => [
            'mapping_type' => self::HAS_MANY,
            'class_name'   => 'View_book_has_type',
            'foreign_key'  => 'book_id',
        ]
    ];

    /**
     * @param   $ids string 要修改的图书的id 例如 1,2,3
     * @param   $act        要修改的状态
     * 修改图书的库存状态
     * 1 : 添加占用状态: 将库存中数量-1 占用库存+1
     * 2 : 占用复原转态: 库存中数量+1   占用库存-1
     * 3 : 占用状态转借出: 借出数量+1  占用数量-1
     * 4 : 借出状态转缺失: 借出数量-1 缺失数量+1
     * 5 : 借出状态 还原库存: 借出数量-1 库存数量+1
     * return int 400 失败 200 成功
     */
    public function bookStatus($ids,$act){
        $this->startTrans();
        switch($act){
            case '1':
                /*添加占用状态: 将库存中数量-1 占用库存+1*/
             foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_rent,stock_zhan")->find($v);
                    $book['stock_rent']--;
                    $book['stock_zhan']++;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
            case '2':
                /*占用复原转态: 库存中数量+1   占用库存-1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_rent,stock_zhan")->find($v);
                    $book['stock_rent']++;
                    $book['stock_zhan']--;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
            case '3':
                /*占用状态转借出: 借出数量+1  占用数量-1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_zhan,stock_jie")->find($v);
                    $book['stock_zhan']--;
                    $book['stock_jie']++;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
            case '4':
                /*借出转占用: 借出数量-1  占用数量+1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_zhan,stock_jie")->find($v);
                    $book['stock_zhan']++;
                    $book['stock_jie']--;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
                break;
            case '5':
                /*借出状态转缺失: 借出数量-1 缺失数量+1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_jie,stock_que")->find($v);
                    $book['stock_jie']--;
                    $book['stock_que']++;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
            case '6':
                /*借出状态 还原库存: 借出数量-1 库存数量+1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_jie,stock_rent")->find($v);
                    $book['stock_jie']--;
                    $book['stock_rent']++;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
            case '7':
                /*库存状态 转借出:  库存数量-1 借出数量+1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_jie,stock_rent")->find($v);
                    $book['stock_jie']++;
                    $book['stock_rent']--;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
            case '8':
                /*报废 缺失-1 库存-1*/
                foreach($ids as $k=>$v){
                    $book = $this->field("book_id,stock_que,stock_rent")->find($v);
                    $book['stock_que']--;
                    $book['stock_rent']--;
                    $key = $this->save($book);
                    if(!$key){
                        $this->rollback();
                        return 400;
                    }
                }
            break;
        }
        return $this;
    }

}